package com.wuhongyu.controller;

import com.wuhongyu.common.MyCommon;
import com.wuhongyu.entity.Server;
import com.wuhongyu.entity.User;
import com.wuhongyu.service.DictionaryService;
import com.wuhongyu.service.ServerService;
import com.wuhongyu.service.UserService;
import com.wuhongyu.service.impl.DictionaryServiceImpl;
import com.wuhongyu.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 服务
 */
@Controller
@RequestMapping("/server")
public class ServerController {

    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private ServerService serverService;
    @Autowired
    private UserService userService;

    /**
     * 服务归档
     *
     * @param model
     * @param request
     * @param server
     * @param number
     * @return
     */
    @RequestMapping("/arch")
    public String arch(Model model, HttpServletRequest request, Server server, Integer number) {
        //服务归档
        if (server.getStatus() == null) {
            server.setStatus(4);
        }
        model.addAttribute("serverList", ((DictionaryServiceImpl) dictionaryService).findByType(MyCommon.SERVER));
        model.addAttribute("url", MyUtil.getUrl(request));
        model.addAttribute("page", serverService.findAll(server, number));
        return "~cust/service/arch";
    }

    /**
     * 服务反馈
     *
     * @param model
     * @param request
     * @param server
     * @param number
     * @return
     */
    @RequestMapping("/feedback")
    public String feedback(Model model, HttpServletRequest request, Server server, Integer number) {
        //服务反馈
        server.setStatus(3);
        model.addAttribute("serverList", ((DictionaryServiceImpl) dictionaryService).findByType(MyCommon.SERVER));
        model.addAttribute("url", MyUtil.getUrl(request));
        model.addAttribute("page", serverService.findAll(server, number));
        return "~cust/service/feedback";
    }

    /**
     * 服务处理
     *
     * @param model
     * @param request
     * @param server
     * @param number
     * @return
     */
    @RequestMapping("/deal")
    public String deal(Model model, HttpServletRequest request, Server server, Integer number) {
        //服务处理
        server.setStatus(2);
        model.addAttribute("serverList", ((DictionaryServiceImpl) dictionaryService).findByType(MyCommon.SERVER));
        model.addAttribute("url", MyUtil.getUrl(request));
        model.addAttribute("page", serverService.findAll(server, number));
        return "~cust/service/deal";
    }

    /**
     * 服务分配
     *
     * @param model
     * @param request
     * @param server
     * @param number
     * @return
     */
    @RequestMapping("/dispatch")
    public String dispatch(Model model, HttpServletRequest request, Server server, Integer number) {
        server.setStatus(1);
        model.addAttribute("userList", userService.findAllExit());
        model.addAttribute("serverList", ((DictionaryServiceImpl) dictionaryService).findByType(MyCommon.SERVER));
        model.addAttribute("url", MyUtil.getUrl(request));
        model.addAttribute("page", serverService.findAll(server, number));
        return "~cust/service/dispatch";
    }

    /**
     * 去分配
     *
     * @param model
     * @param id
     * @param userid
     * @return
     */
    @RequestMapping("/toDispatch")
    public String toDispatch(Model model, Integer id, Integer userid) {
        User user = userService.findOne(userid);
        Server server = serverService.findOne(id);
        server.setAppointUser(user);
        server.setStatus(1);
        serverService.save(server);
        return "redirect:/server/dispatch";
    }

    /**
     * 服务查看
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/query")
    public String query(Model model, Integer id) {
        model.addAttribute("server", serverService.findOne(id));
        return "~cust/service/detail";
    }

    /**
     * 服务处理
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/queryDeal")
    public String queryDeal(Model model, Integer id) {
        model.addAttribute("server", serverService.findOne(id));
        return "~cust/service/deal_detail";
    }

    /**
     * 查看服务反馈
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/queryFeedback")
    public String queryFeedback(Model model, Integer id) {
        model.addAttribute("server", serverService.findOne(id));
        return "~cust/service/feedback_detail";
    }

    /**
     * 添加服务
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/queryBefore")
    public String queryBefore(Model model, Integer id) {
        model.addAttribute("serverList", ((DictionaryServiceImpl) dictionaryService).findByType(MyCommon.SERVER));
        return "~cust/service/add";
    }


    /**
     * 各种服务操作的保存
     * @param model
     * @param request
     * @param server
     * @return
     */
    @RequestMapping("/save")
    public String save(Model model, HttpServletRequest request, Server server) {
        if (server.getStatus() == null) {
            serverService.save(server);
            return "redirect:/server/queryBefore";
        } else if (server.getStatus() == 2) {
            serverService.save(server);
            return "redirect:/server/deal";
        } else if (server.getStatus() == 3) {
            serverService.save(server);
            return "redirect:/server/feedback";
        } else {
            return "redirect:/server/arch";
        }
    }

    @RequestMapping("/delete")
    public String delete(Model model, HttpServletRequest request, Integer id) {
        serverService.delete(id);
        return "redirect:/server/dispatch";
    }
}